package cn.edu.zucc.JavaChat.ChatServer.DB.DAO.Imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.edu.zucc.JDBCUtil.DBConnection;

public abstract class AbstractDAO {
	private Connection con;
	protected Connection connect(){
		return con = DBConnection.getConnection();
	}
	protected void closeConnection(){
		if(con == null) return;
		DBConnection.releaseConnection(con);
		con = null;
	}
	protected boolean DBOperationUpdate(String sql){
		boolean result = false;
		Connection con = this.connect();
		try {
			con.setAutoCommit(false);
			PreparedStatement p = con.prepareStatement(sql);
			p.executeUpdate();
			con.commit();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		finally{
			this.closeConnection();
		}
		return result;
	}

}
